﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

using Ext.Net;
using BG.Data;
using BG.Service;
using Kwan.Helper.Bitmaps;
using Kwan.Data;

namespace BG.Web.Admin.Collection
{
    public partial class Default : UIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                //BtnAddClassInfo.Hidden = !permit[0];
                //AppendChild.Hidden = !permit[0];
                //BtnAddInfo.Hidden = !permit[0];
                //Rename.Hidden = !permit[2];
                //btnChangeHot.Hidden = !permit[2];

                //BtnDeleteInfo.Hidden = !permit[3];
                //Remove.Hidden = !permit[3];
                //if (permit[3])
                //{
                //    List<GridCommand> gridCommand = new List<GridCommand>{
                //        ExtHelper.CreateGridCommand("Delete", Icon.Delete, "删除", "删除该条记录")
                //    };
                //    GridPanel1.ColumnModel.Columns.Add(ExtHelper.CreateCommandColumn(gridCommand));
                //}
                //ResourceManager1.RegisterIcon(Icon.Image);
                qStartDate.Text = DateTime.Now.ToShortDateString();
                qEndDate.Text = DateTime.Now.ToShortDateString();
            }
        }

        protected void BtnAddInfo_Click(object sender, EventArgs e)
        {
            SetEditWindow(new TCollection());
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            string[] list = obj.Split(',');
            (TCollection.entityID == list).Delete<TCollection>();
            Store1.DataBind();
        }

        [DirectMethod]
        public void ChangeState(string obj, string NameValue)
        {
            IList<TCollection> list = JSON.Deserialize<IList<TCollection>>(obj);
            if (list.Count > 0)
            {
                CrudManage.ChangeState(list, NameValue);
                Store1.DataBind();
            }
            else
            {
                ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
        }

        [DirectMethod]
        public void EditInfo(string EntityID)
        {
            if (!permit[2]) return;
            SetEditWindow((TCollection.entityID == int.Parse(EntityID)).ListFirst<TCollection>());
            EditWindow.Show();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());

            //搜索项
            if (string.IsNullOrEmpty(txtFilter.Text))
                ps.Set("tag", string.Format(",{0},{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")));
            else
                ps.Set("tag", txtFilter.Text);

            ps.Set("pid", "0");
            //ps.Set("tag", txtFilter.Text);

            int pageindex = e.Start / e.Limit;
            Kwan.Data.Region r = new Kwan.Data.Region(pageindex, e.Limit);

            Expression exp = CollectionQuery.MakeSearchWhere(ps);
            IList<VCollection> ListView = exp.List<VCollection>(r, new string[] { e.Sort + " " + e.Dir });

            e.Total = new Expression().Count<TCollection>();
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void SetEditWindow(TCollection tt)
        {
            txtID.Text = tt.EntityID.ToString();
            //txtEnable.Checked = tt.Enable;
            //txtHot.Checked = tt.Hot;;
			txtBillNO.Text = tt.BillNO;
			txtCashPay.Text = tt.CashPay.ToString();
			txtChange.Text = tt.Change.ToString();
			txtCreatedDate.Text = tt.CreatedDate.ToString();
			//txtCustomerID.Text = tt.CustomerID.ToString();
			txtMemo.Text = tt.Memo;
			//txtOrderID.Text = tt.OrderID.ToString();
			txtPaid.Text = tt.Paid.ToString();
			txtPosPay.Text = tt.PosPay.ToString();
			txtReceivable.Text = tt.Receivable.ToString();
			//txtRoomID.Text = tt.RoomID.ToString();
			//txtRoomNO.Text = tt.RoomNO;
			txtStatus.Text = tt.Status.ToString();
			//txtType.Text = tt.Type;
			//txtUserID.Text = tt.UserID;
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            TCollection tt = Convert.ToInt32(txtID.Text) > 0 ? 
                (TCollection.entityID == Convert.ToInt32(txtID.Text)).ListFirst<TCollection>() : new TCollection();

			tt.BillNO = txtBillNO.Text;
			tt.CashPay = double.Parse(txtCashPay.Text);
			tt.Change = double.Parse(txtChange.Text);
			tt.CreatedDate = DateTime.Now;
			//tt.CustomerID = int.Parse(txtCustomerID.Text);
			tt.Memo = txtMemo.Text;
			//tt.OrderID = int.Parse(txtOrderID.Text);
			tt.Paid = double.Parse(txtPaid.Text);
			tt.PosPay = double.Parse(txtPosPay.Text);
			tt.Receivable = double.Parse(txtReceivable.Text);
			//tt.RoomID = int.Parse(txtRoomID.Text);
			//tt.RoomNO = txtRoomNO.Text;
			tt.Status = int.Parse(txtStatus.Text);
			//tt.Type = txtType.Text;
			//tt.UserID = txtUserID.Text;
            //tt.Enable = txtEnable.Checked;
            //tt.Hot = txtHot.Checked;

            DBContext.Save(tt);
            Store1.DataBind();
            Store1.Dispose();
            EditWindow.Hide();
        }
        [DirectMethod]
        public void CorrectOrder(string obj)
        {
            string[] list = obj.Split(',');
            if (permit[4])
            {
                using (IConnectinContext cc = DBContext.Context1)
                {
                    foreach (string str_id in list)
                    {
                        int collectID = int.Parse(str_id);
                        IList<TCollection> collection = (TCollection.entityID == collectID).List<TCollection>();
                        if (collection.Count > 0)
                        {
                            TOrder order = (TOrder.entityID == collection[0].OrderID).ListFirst<TOrder>();
                            if (order != null && order.Status == 2)
                            {
                                TRoom room = (TRoom.entityID == order.RoomID).ListFirst<TRoom>();
                                if (room != null)
                                {
                                    room.Status = 1;
                                    order.Status = 3;
                                    DBContext.Save(order);
                                    DBContext.Save(room);
                                }
                                IList<TConsume> consume = (TConsume.orderID == order.EntityID).List<TConsume>();
                                foreach (TConsume cs in consume)
                                {
                                    cs.Status = 3;
                                    DBContext.Save(cs);
                                }
                                foreach (TCollection cl in collection)
                                {
                                    cl.Status = 3;
                                    DBContext.Save(cl);
                                }
                                Store1.DataBind();
                            }
                        }
                    }
                }
            }
        }
    }
}